package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment;
import com.atguigu.springcloud.entities.construct.ResultReturn;
import com.atguigu.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 支付模块
 * @author zxl
 *
 */
@RestController
@RequestMapping("payment")
@Slf4j
public class PaymentController {

    @Autowired
    private PaymentService paymentService;

    // 服务发现端，可以通过这个类来获得该服务的信息
    @Resource
    private DiscoveryClient discoveryClient;

    @Value("${server.port}")
    private String serverPort;

    @PostMapping("/create")
    public CommonResult create( @RequestBody Payment payment ){
        log.info("服务端接受到的数据"+ payment.getSerial());
        int i = paymentService.create(payment);
        if ( i > 0 ) {
            return new CommonResult(ResultReturn.success,"插入成功，serverPort:" + serverPort ,i);
        } else {
            return new CommonResult(ResultReturn.error,"插入失败，serverPort:" + serverPort , null);
        }
    }

    @GetMapping("/getPaymentById/{id}")
    public CommonResult getPaymentById(@PathVariable("id") long id){
        Payment payment = paymentService.getPaymentById(id);
        log.info("查询结果集:"+ payment);
        if ( payment != null ) {
            return new CommonResult(ResultReturn.success,"查询成功，serverPort:" + serverPort ,payment);
        } else {
            return new CommonResult(ResultReturn.error,"查询失败，serverPort:" + serverPort , null);
        }
    }

    @GetMapping("/get/getPaymentById/{id}")
    public CommonResult getPaymentByGetId(@PathVariable("id") long id){
        Payment payment = paymentService.getPaymentById(id);
        log.info("查询结果集:"+ payment);
        if ( payment != null ) {
            return new CommonResult(ResultReturn.success,"查询成功，serverPort:" + serverPort ,payment);
        } else {
            return new CommonResult(ResultReturn.error,"查询失败，serverPort:" + serverPort , null);
        }
    }

    /**
     * 获取服务发现端 相关信息
     * @return
     */
    @GetMapping("/discovery")
    public Object discovery () {
        // 获得注册金eureka中的所有微服务列表
        List<String> services = discoveryClient.getServices();
        for ( String element : services ) {
            log.info("********element: " + element );
        }

        // 通过微服务名 获取更详细的微服务信息
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");// 这里必须是大写
        for ( ServiceInstance instance : instances ) {
            log.info( instance.getServiceId()  + "\t" + instance.getHost() +"\t"+ instance.getPort() +"\t"+ instance.getUri());
        }
        return this.discoveryClient;
    }

    /**
     * 返回当前端口号
     * @return
     */
    @GetMapping("/lb")
    public String getPaymentLB(){
        return serverPort;
    }

    /**
     * payment测试的超市程序
     * @return
     */
    @GetMapping("feign/timeout")
    public String paymentFeignTimeout(){

        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return serverPort;
    }

    /**
     * 测试zipkin
     * @return
     */
    @GetMapping("/zipkin")
    public String getPaymenZipKin(){
        return "hi，i、am paymentzipkin server fall back,welcome to guigu haha";
    }
}
